package com.ruoyi.buss.grade.service.impl;

import java.text.SimpleDateFormat;
import java.util.*;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.buss.task.domain.DataPlanTaskRelation;
import com.ruoyi.buss.task.domain.DataTaskInfo;
import com.ruoyi.buss.task.domain.DataUserTaskRelation;
import com.ruoyi.buss.task.mapper.DataTaskInfoMapper;
import com.ruoyi.buss.utils.DateUtilss;
import com.ruoyi.buss.utils.GetOnlyId;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.api.model.LoginUser;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.buss.grade.mapper.DataCustGradeInfoMapper;
import com.ruoyi.buss.grade.domain.DataCustGradeInfo;
import com.ruoyi.buss.grade.service.IDataCustGradeInfoService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 个人维度表Service业务层处理
 * 
 * @author sxy
 * @date 2022-04-25
 */
@Service
public class DataCustGradeInfoServiceImpl implements IDataCustGradeInfoService 
{
    @Resource
    private DataCustGradeInfoMapper dataCustGradeInfoMapper;

    @Resource
    private DataTaskInfoMapper dataTaskInfoMapper;

    /**
     * 查询个人维度表
     * 
     * @param idCard 个人维度表ID
     * @return 个人维度表
     */
    @Override
    public DataCustGradeInfo selectDataCustGradeInfoById(String idCard)
    {
        return dataCustGradeInfoMapper.selectDataCustGradeInfoById(idCard);
    }

    /**
     * 查询个人维度表列表
     * 
     * @param dataCustGradeInfo 个人维度表
     * @return 个人维度表
     */
    @Override
    public List<DataCustGradeInfo> selectDataCustGradeInfoList(DataCustGradeInfo dataCustGradeInfo)
    {
        return dataCustGradeInfoMapper.selectDataCustGradeInfoList(dataCustGradeInfo);
    }

    /**
     * 新增个人维度表
     * 
     * @param dataCustGradeInfo 个人维度表
     * @return 结果
     */
    @Override
    public int insertDataCustGradeInfo(DataCustGradeInfo dataCustGradeInfo)
    {
        dataCustGradeInfo.setCreateTime(DateUtils.getNowDate());
        return dataCustGradeInfoMapper.insertDataCustGradeInfo(dataCustGradeInfo);
    }

    /**
     * 修改个人维度表
     * 
     * @param dataCustGradeInfo 个人维度表
     * @return 结果
     */
    @Override
    public int updateDataCustGradeInfo(DataCustGradeInfo dataCustGradeInfo)
    {
        return dataCustGradeInfoMapper.updateDataCustGradeInfo(dataCustGradeInfo);
    }

    /**
     * 批量删除个人维度表
     * 
     * @param idCards 需要删除的个人维度表ID
     * @return 结果
     */
    @Override
    public int deleteDataCustGradeInfoByIds(String[] idCards)
    {
        return dataCustGradeInfoMapper.deleteDataCustGradeInfoByIds(idCards);
    }

    /**
     * 删除个人维度表信息
     * 
     * @param idCard 个人维度表ID
     * @return 结果
     */
    @Override
    public int deleteDataCustGradeInfoById(String idCard)
    {
        return dataCustGradeInfoMapper.deleteDataCustGradeInfoById(idCard);
    }

    /**
     * 根据条件获取当前方案客户数
     *
     * @param dataCustGradeInfo
     * @return
     */
    @Override
    public int selectDataCustGradeInfoCount(DataCustGradeInfo dataCustGradeInfo) {

        if(StringUtils.isNotEmpty(dataCustGradeInfo.getKhqt()) &&
                !"[]".equals(dataCustGradeInfo.getKhqt())
        ){
            //将json数组转回数组
            JSONArray khqtArr= JSON.parseArray(dataCustGradeInfo.getKhqt());
            dataCustGradeInfo.setLabelList(JSONObject.parseArray(khqtArr.toJSONString(), String.class));

        }
        return dataCustGradeInfoMapper.selectDataCustGradeInfoCount(dataCustGradeInfo);
    }

    /**
     * 根据条件生成支行营销任务
     *
     * @param dataCustGradeInfo
     * @return
     */
    @Override
    @Transactional
    public int insertTask(DataCustGradeInfo dataCustGradeInfo, LoginUser userInfo) {
        DataTaskInfo taskInfo = new DataTaskInfo();
        String orgId = userInfo.getSysUser().getDept().getOrgId();
        String userId = userInfo.getSysUser().getUserName();

        taskInfo.setCreateUser(userId);
        taskInfo.setUpdateUser(userId);
        taskInfo.setUserId(userId);
        taskInfo.setOrgId(orgId);
        taskInfo.setFinishNum(0);// 新添加的任务，完成数量为0
        // 获取ID
        Map<String,String> map = new HashMap<>();
        String taskId = GetOnlyId.getOnlyId();
        taskInfo.setTaskId(taskId);

        // 设置任务开始时间和结束时间，其中开始时间是当天第一秒，结束时间是当天最后一秒
        taskInfo.setBeginDate(dataCustGradeInfo.getBeginDate());
        taskInfo.setEndDate(dataCustGradeInfo.getEndDate());
        taskInfo.setCreateTime(DateUtils.getNowDate());
        taskInfo.setStatus("1"); //有效  0无效
        taskInfo.setTaskType("2"); //2支行行长创建任务
        taskInfo.setIfGroup("0"); //默认0
        taskInfo.setComplete("0");//默认0
        taskInfo.setTaskName(dataCustGradeInfo.getTaskName());

        int told = dataTaskInfoMapper.insertDataTaskInfo(taskInfo);

        dataCustGradeInfo.setTaskId(taskId);
        if(StringUtils.isNotEmpty(dataCustGradeInfo.getKhqt()) &&
                !"[]".equals(dataCustGradeInfo.getKhqt())
            ){
            //将json数组转回数组
            JSONArray khqtArr= JSON.parseArray(dataCustGradeInfo.getKhqt());
            dataCustGradeInfo.setLabelList(JSONObject.parseArray(khqtArr.toJSONString(), String.class));
//            List<String> labelList = JSONObject.parseArray(khqtArr.toJSONString(), String.class);

        }

        return dataCustGradeInfoMapper.insertTask(dataCustGradeInfo);
    }
}
